package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;
import com.yqg.education.entity.ExamLog;
import com.yqg.education.mapper.ExamLogMapper;
import com.yqg.education.service.IExamLogService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ExamLogController 前端控制器
 * @author 张芸博
 * @since 2025-04-07
 */
@RestController
@RequestMapping("/exam-log")
public class ExamLogController {

    @Resource
    private IExamLogService examLogService;

    @Resource
    private ExamLogMapper examLogMapper;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody ExamLog examLog) {
        return Result.success(examLogService.saveOrUpdate(examLog));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        ExamLog examLog =examLogService.getById(id);
        return Result.success(examLogService.saveOrUpdate(examLog));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<ExamLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<ExamLog> list=  examLogService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(examLogService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String username,
                           @RequestParam(required = false) Integer trainClassId,
                           @RequestParam(required = false) Integer courseId,
                           @RequestParam(required = false) Integer isPass,
                           @RequestParam(required = false) Integer stuId) {
        pageNum = pageNum * pageSize - pageSize;
        List<ExamLog> exampaperList = examLogMapper.findPage(pageNum, pageSize, username, trainClassId, courseId, isPass, stuId);
        Long count = examLogMapper.findCount(username, trainClassId, courseId, isPass, stuId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", exampaperList);
        map.put("total", count);
        return Result.success(map);
    }
    /**
     * 学员端获取考试信息
     * @param name 模糊匹配
     * @param typeId 类型ID
     * @param userId 用户ID
     * @param flow 考试完成进度 0未完成；1已完成
     */
    @GetMapping("/stuFindExamPage")
    public Result stuFindExamPage(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam(required = false) String name,
                                  @RequestParam(required = false) Integer typeId,
                                  @RequestParam Integer userId,
                                  @RequestParam(required = false) Integer flow){
        Map<String,Object> map = examLogService.findExamPage(pageNum,pageSize,name,typeId,userId,flow);
        return Result.success(map);
    }

    /**
     * 学员端查看成绩页面接口
     */
    @GetMapping("/stuFindExamScore")
    public Result stuFindExamScore(@RequestParam Integer userId,
                                   @RequestParam Integer courseId){

        return Result.success(examLogService.findExamScore(userId,courseId));
    }

    /**
     * 查看排名
     */
    @GetMapping("/getRankByCourseId")
    public Result getRankByCourseId(@RequestParam BigDecimal exampaperGetScore,
                                   @RequestParam Integer userId,
                                   @RequestParam Integer courseId){

        return Result.success(examLogMapper.getRank(exampaperGetScore,userId,courseId));
    }
}

